package com.example.netty.bio;

import io.netty.util.CharsetUtil;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.Scanner;

/**
 * @Author: 江涛
 * @CreateTime: 2022-09-26  15:03
 * @Description:
 */
public class BioClient {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            socket.connect(new InetSocketAddress("127.0.0.1",6666));
            System.out.println("客户端连接成功");
            //读
            new Thread(()->{
                try {
                    InputStream inputStream = socket.getInputStream();
                    byte[] bytes = new byte[1024];
                    int read = inputStream.read(bytes);
                    while (read != -1) {
                        System.out.println(new String(bytes,0,read));
                        read = inputStream.read(bytes);
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }).start();

            //写
            while (scanner.hasNext()) {
                socket.getOutputStream().write(scanner.nextLine().getBytes());
            }

        }

    }
}
